package com.novartis.nims.imagedc.application;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.text.ParseException;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import com.novartis.nims.imagedc.util.FormattedTextFieldVerifier;
import com.novartis.nims.imagedc.util.RegexPatternFormatter;

public class InputDirectoryPanel extends JPanel {
	
	private static final long serialVersionUID = 1332366450884303642L;
	
	private JPanel fileChooserPanel;
	private JPanel studyIdPanel;
	private JFileChooser chooser;
	private JFormattedTextField studyIdField;
	private JLabel studyIdInvalidLabel;
	
	public InputDirectoryPanel() {
		JLabel chooserLabel = new JLabel();
		chooserLabel.setText("Select one or more directories containing the image files for this study");
		chooserLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		fileChooserPanel = new JPanel();
		fileChooserPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		fileChooserPanel.setLayout(new BorderLayout());
		fileChooserPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		studyIdPanel = new JPanel();
		studyIdPanel.setLayout(new BoxLayout(studyIdPanel, BoxLayout.PAGE_AXIS));
		
		chooser = new JFileChooser();
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setControlButtonsAreShown(false);
		fileChooserPanel.add(chooser, BorderLayout.CENTER);
		fileChooserPanel.setMinimumSize(new Dimension(0, 0));
		
		JLabel label = new JLabel();
		label.setText("Enter the study ID (of the form CXXX000X0000)");
		label.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		studyIdField = new JFormattedTextField(new RegexPatternFormatter(Pattern.compile("^(()|(C[A-Z]{3,4}[0-9]{3}(([A-Z]{0,1}[0-9]{4,5})|([A-Z]{3}[0-9]{2}))))$")));
		int height = studyIdField.getPreferredSize().height;
		studyIdField.setMaximumSize(new Dimension(200, height));
		studyIdField.setMinimumSize(new Dimension(200, height));
		studyIdField.setAlignmentX(Component.LEFT_ALIGNMENT);
		studyIdField.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent event) { }

			public void focusLost(FocusEvent event) {
				try {
					InputDirectoryPanel.this.studyIdField.commitEdit();
				} catch (ParseException e) { }
			}
			
		});
		
		studyIdInvalidLabel = new JLabel();
		studyIdInvalidLabel.setFont(studyIdInvalidLabel.getFont().deriveFont(Font.ITALIC, 10));
		studyIdInvalidLabel.setText("This Study ID is invalid. You must fix it before continuing");
		studyIdInvalidLabel.setVisible(false);

		studyIdField.setInputVerifier(new FormattedTextFieldVerifier(studyIdInvalidLabel));
		
		studyIdPanel.add(studyIdField);
		studyIdPanel.add(studyIdInvalidLabel);

		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.add(chooserLabel);
		this.add(Box.createRigidArea(new Dimension(1, 10)));
		this.add(fileChooserPanel);
		this.add(Box.createRigidArea(new Dimension(1, 10)));
		this.add(label);	
		this.add(studyIdPanel);	
		this.add(Box.createRigidArea(new Dimension(1, 10)));
	}

	public File getSelectedDirectory() {
		return chooser.getSelectedFile();
	}

	public String getStudyId() {
		return studyIdField.getText();
	}
	
	public boolean isInputValid() {
		if (getSelectedDirectory() == null) {
			return false;
		}
		
		if (getStudyId() == null || getStudyId().equals("") /*|| !studyIdField.isEditValid()*/) {
			return false;
		}
		
		return true;
	}

}
